package bb.lanxing.activity.agreement

import android.content.Context
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import bb.lanxing.R
import bb.lanxing.activity.base.BaseActivity
import bb.lanxing.common.config.Constants
import bb.lanxing.utils.IntentUtils

open class AgreementActivity : BaseActivity() {
    protected fun getSpannableStringBuilder(context: Context): SpannableStringBuilder {
        val spannableStringBuilder = SpannableStringBuilder()
        val read = getString(R.string.user_service_agreement)
        val policy = StringBuilder(getString(R.string.character_left)).append(getString(R.string.dialog_privacy_policy)).append(getString(R.string.character_right)).toString()
        val service = StringBuilder(getString(R.string.character_left)).append(getString(R.string.dialog_service_agreement)).append(getString(R.string.character_right)).toString()
        val and = getString(R.string.user_service_agreement_and)
        spannableStringBuilder.append(read)
        spannableStringBuilder.append(policy)
        spannableStringBuilder.append(and)
        spannableStringBuilder.append(service)
        val foregroundColorSpan = ForegroundColorSpan(resources.getColor(R.color.global_blue_color))
        val start1 = read.length
        val end1 = start1 + policy.length
        val start2 = end1 + and.length
        val end2 = start2 + service.length
        spannableStringBuilder.setSpan(foregroundColorSpan, start1, end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spannableStringBuilder.setSpan(foregroundColorSpan, start2, end2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spannableStringBuilder.setSpan(object : ClickableSpan() {
            override fun onClick(view: View) {
                IntentUtils.gotoWebBrowser(
                    context, Constants.URL_PRIVACY_AGREEMENT, null
                )
            }
        }, start1, end1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spannableStringBuilder.setSpan(object : ClickableSpan() {
            override fun onClick(view: View) {
                IntentUtils.gotoWebBrowser(
                    context, Constants.URL_USER_AGREEMENT, null
                )
            }
        }, start2, end2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return spannableStringBuilder
    }
}